package xhwl.insurance.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.AnimationDrawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.umeng.socialize.utils.Log;

import java.io.IOException;
import java.util.List;

import xhwl.insurance.R;
import xhwl.insurance.http.bean.resp.ForumProductResp;
import xhwl.insurance.ui.ForumDetailNewActivity;
import xhwl.insurance.ui.voicesupport.MediaPlay;
import xhwl.insurance.util.ImgTools;
import xhwl.insurance.view.ForumPictureView;
import xhwl.pulltorefresh.adapter.BaseAdapter;
import xhwl.pulltorefresh.adapter.ViewHolder;

/**
 * Created by Administrator on 2017/10/9.
 * 论坛帖子列表adapter
 */

public class ForumListNewAdapter extends BaseAdapter<ForumProductResp> {
    private Context context;
    List<ForumProductResp> forumList;
    OnItemClickLitener onItemClickLitener;
    private String[] picUrl;
    private String mPicUrl;
    private LinearLayout ll_voice;

    public ForumListNewAdapter(Context context, List<ForumProductResp> forumList) {
        super(context, R.layout.forum_item, forumList);
        this.context = context;
        this.forumList = forumList;

    }

    @Override
    public void convert(final ViewHolder holder, final ForumProductResp forumProductResp) {
        holder.setText(R.id.tv_forum_title, forumProductResp.getPOST_TITLE());//论坛title
        holder.setText(R.id.tv_name, forumProductResp.getMEMBER_NAME());//姓名
        holder.setText(R.id.tv_forum_time, forumProductResp.getISSUE_DATE());//时间
        holder.setText(R.id.tv_forum_content, forumProductResp.getPOST_DETAILS());//内容
        final ImageView touxiang = (ImageView) holder.getView(R.id.touxiang);
        ll_voice = (LinearLayout) holder.getView(R.id.ll_voice);
        if (forumProductResp.getVOICE_LONG()!=null) {
            holder.setText(R.id.voice_display_voice_time, forumProductResp.getVOICE_LONG()+"\"");//语音时长
        }else{
            holder.setText(R.id.voice_display_voice_time,"未知");
        }


        ImageView voice_display_voice_play=(ImageView)holder.getView(R.id.voice_display_voice_play);
        ImgTools.getInstance().getImgFromNetByUrl(forumProductResp.getMEMBER_PIC(), touxiang, R.drawable.touxiang);
        final ForumPictureView forumPicView = (ForumPictureView) holder.getView(R.id.forumPicView);
      //0 图文 1 语音 2视频
        if ("1".equals(forumProductResp.getPOST_TYPE())){
            ll_voice.setVisibility(View.VISIBLE);
            forumPicView.setVisibility(View.GONE);
        }else{
            ll_voice.setVisibility(View.GONE);
            forumPicView.setVisibility(View.VISIBLE);
        }
        if ("0".equals(forumProductResp.getPOST_TYPE())) {
            forumPicView.setData(forumProductResp.getVI_PI_URL(), 3);
        }else{
            forumPicView.setData2(forumProductResp);
        }
        // 如果设置了回调，则设置点击事件
        final int pos = holder.getLayoutPosition();
        holder.setText(R.id.tv_forum_pinglun, forumProductResp.getREPLY_NUM());//回复数量
        holder.setText(R.id.tv_forum_goodnum, forumProductResp.getTHUMBS_UP());
        final TextView textview = holder.getView(R.id.tv_forum_goodnum);//点赞数量
        final ImageView imageview = (ImageView) holder.getView(R.id.iv_forum_good);//点赞图片




        if (forumProductResp.getTHUMBS_FLAG().equals("1")) {
            imageview.setImageResource(R.drawable.icon_thumbs_up2);
        } else {
            imageview.setImageResource(R.drawable.icon_thumbs_up);
        }

        holder.setOnclickListener(R.id.ll_forum_detail_item, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent();
                intent.setClass(context, ForumDetailNewActivity.class);
                intent.putExtra(ForumDetailNewActivity.EXTRA_DATA, forumProductResp);
                context.startActivity(intent);
            }
        });
        holder.setOnclickListener(R.id.ll_pinglun_num, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent();
                intent.setClass(context, ForumDetailNewActivity.class);
                intent.putExtra(ForumDetailNewActivity.EXTRA_DATA, forumProductResp);
                context.startActivity(intent);
            }
        });


        holder.setOnclickListener(R.id.iv_forum_good, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                onItemClickLitener.onGoodClick(pos - 1, imageview, textview);

            }
        });
        holder.setOnclickListener(R.id.iv_share, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                onItemClickLitener.onShareClick(pos - 1);
            }
        });

        holder.setOnclickListener(R.id.voice_display_voice_play, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                onItemClickLitener.onVoicePlay(pos - 1,holder.itemView);

            }
        });
    }



    public interface OnItemClickLitener {
        void onItemClick(int position);

        void onShareClick(int position);

        void onGoodClick(int position, ImageView imageview, TextView textview);

        void onVoicePlay(int position,View mView);

    }

    public void setOnItemClickLitener(OnItemClickLitener onItemClickLitener) {
        this.onItemClickLitener = onItemClickLitener;
    }

}
